package com.sgq.vue.aspect;

import com.sgq.vue.annotation.PlateDB;
import com.sgq.vue.entity.picture.Plate;
import com.sgq.vue.entity.system.Permission;
import com.sgq.vue.service.picture.PlateService;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author: 83778
 * @date: 2024-01-04 19:19
 * @desc:
 */
@Aspect
@Component
public class PlateAspect {
    @Autowired
    private PlateService plateService;

    @Pointcut(value = "@annotation(com.sgq.vue.annotation.PlateDB)")
    public void platePointCut() {

    }

    /**
     *  在添加图片分区菜单时，添加图片分区
     * @param joinPoint
     * @param plateDB
     */
    @Around("platePointCut() && @annotation(plateDB)")
    public Object managePlate(ProceedingJoinPoint joinPoint, PlateDB plateDB) {
        Object result ;
        try {
            result = joinPoint.proceed();
        } catch (Throwable throwable) {
            result = null;
            throwable.printStackTrace();
        }
        Permission permission = (Permission) result ;
        Long parentId = permission.getParentId();
        String value = plateDB.value();
        if (parentId.equals(49L)){
            if ("add".equals(value)){
                System.out.println("执行添加操作。。。");
                Plate plate = new Plate();
                plate.setId(permission.getId());
                plate.setTotal(0);
                plate.setName(permission.getLabel());
                plateService.save(plate);
            }else if ("edit".equals(value)){
                System.out.println("执行修改操作。。。");
                Plate plate = plateService.getById(permission.getId());
                plate.setName(permission.getLabel());
                plateService.updateById(plate);
            }else if ("delete".equals(value)){
                System.out.println("执行删除操作。。。");
                plateService.removeById(permission.getId());
            }
        }

        return result;

    }
}
